package com.cost.service.impl;

import com.cost.common.Monthly;
import com.cost.common.Status;
import com.cost.dao.CostConsumeDao;
import com.cost.service.CostConsumeService;
import com.cost.vo.Entity;
import com.rmb.mart.entity.cost.CostConsume;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (CostConsume)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 14:04:16
 */
@Service("costConsumeService")
public class CostConsumeServiceImpl implements CostConsumeService {
    @Resource
    private CostConsumeDao costConsumeDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CostConsume queryById(Long id) {
        return this.costConsumeDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CostConsume> queryAllByLimit(int offset, int limit) {
        return this.costConsumeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param costConsume 实例对象
     * @return 实例对象
     */
    @Override
    public CostConsume insert(CostConsume costConsume) {
        this.costConsumeDao.insert(costConsume);
        return costConsume;
    }

    /**
     * 修改数据
     *
     * @param costConsume 实例对象
     * @return 实例对象
     */
    @Override
    public CostConsume update(CostConsume costConsume) {
        this.costConsumeDao.update(costConsume);
        return this.queryById(costConsume.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.costConsumeDao.deleteById(id) > 0;
    }

    /**
     * 查询所用数据
     *
     * @return 所用数据
     */

    @Override
    public List<CostConsume> selectAll() {
        return this.costConsumeDao.queryAll(null);
    }

    /**
     * 条件查询
     * @param costConsume 条件
     * @return 条件查询数据
     */
    @Override
    public List<CostConsume> selectAllCondition(CostConsume costConsume) {

        List<CostConsume> costConsumes = new ArrayList<>();
        if (costConsume != null) {
            Long uid = costConsume.getUid();
            if (uid != null) {
                costConsumes = this.costConsumeDao.queryAll(costConsume);
            }
            return costConsumes;
        } else {
            return costConsumes;
        }
    }

    /**
     * 查询所用金额
     * @return
     */
    @Override
    public Map<String, Object> selectConsume() {
        ArrayList<String> list = new ArrayList<>();
        list.add(Monthly.JANUARY);
        list.add(Monthly.FEBRUARY);
        list.add(Monthly.MARCH);
        list.add(Monthly.APRIL);
        list.add(Monthly.MAY);
        list.add(Monthly.JUNE);
        list.add(Monthly.JULY);
        list.add(Monthly.AUGUST);
        list.add(Monthly.SEPTEMBER);
        list.add(Monthly.OCTOBER);
        list.add(Monthly.NOVEMBER);
        list.add(Monthly.DECEMBER);

        HashMap<String, Object> map = new HashMap<>();
        List<Double> section = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            if (i+1==list.size()){
                section = this.costConsumeDao.section(list.get(list.size()-1),Monthly.MONYH);
            }else {
               section = this.costConsumeDao.section(list.get(i),list.get(i + 1));
            }
            Double mothly = 0.0;
            for (Double aDouble : section) {
                mothly=mothly+aDouble;
            }
            map.put(i+1+"月",mothly);
        }
        return map;
    }

    /**
     * 查询用户数据
     * @param id 用户id
     * @return
     */
    @Override
    public Entity selectFindUid(Long id) {
        return this.costConsumeDao.selectFindUid(id);
    }

    /**
     * 修改法值
     * @param id 用户id
     * @param warningName 预警法值
     * @param newPhone 预警手机号
     */
    @Override
    public void updateIfo(Long id, BigDecimal warningName, String newPhone) {

        Entity entity = this.costConsumeDao.selectFindUid(id);
        Long userWarningId = entity.getUserWarningId();
        this.costConsumeDao.updateInfo(userWarningId, warningName, newPhone);
        HashMap<String, String> map = new HashMap<>(16);
        map.put("phone",newPhone);
        map.put("message","余额小于阀值");
        if (entity.getBalance().compareTo(warningName)==-1) {
            rabbitTemplate.convertAndSend(Status.QUEUE,map);
        }
    }

}